package com.spr.service;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.spr.model.Application;
import com.spr.model.Student;
import com.spr.repository.ApplicationRepository;
import com.spr.repository.StudentRepository;
@Service
public class ApplicationServiceImpl implements ApplicationService {

	
	@Resource
	ApplicationRepository applicationRepository;
	
	@Resource
	StudentRepository studentRepository;
	
	
	@Override
	public Application apply(int studentID, int positionID) {
		Application a = new Application();
		a.setPositionID(positionID);
		a.setStudentID(studentID);
		return applicationRepository.save(a);
	}

	@Override
	public void delete(int applicationID) {
		applicationRepository.delete(applicationID);
		
	}

	@Override
	public List<Student> getStudentsByPositionID(int positionID) {
		List<Application> temp = applicationRepository.findAll();
		List<Application> temp2 = new ArrayList<Application>();
		for(int i = 0; i < temp.size(); i++){
			if(temp.get(i).getPositionID() == positionID){
				temp2.add(temp.get(i));
			}	
		}
		List<Student> students = new ArrayList<Student>();
		int id = 0;
		for(int i = 0; i < temp2.size(); i++){
			id = temp2.get(i).getStudentID();
			students.add(studentRepository.findOne(id));		
		}
		
		
		return students;
	}

}
